package com.example.video.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

import com.example.video.io.database.DatabaseHelper;
import com.example.video.io.database.TBL_App;
import com.example.video.io.database.TBL_Content;

public class VideoProvider extends ContentProvider{

	public static final String TAG="VideoProvider";
	
	public static final String AUTHORITY="com.example.video.provider.VideoProvider";
	
	private final static int OPT_CONTENT=0;
	private final static int OPT_APP=OPT_CONTENT+1;
	
	public static UriMatcher sUriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
	
	static{
		sUriMatcher.addURI(AUTHORITY, TBL_Content.CONTENT, OPT_CONTENT);
		sUriMatcher.addURI(AUTHORITY, TBL_App.APP, OPT_APP);
	}
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return null;
	}

	@Override
	public boolean onCreate() {
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		int match=sUriMatcher.match(uri);
		Cursor cursor=null;
		switch (match) {
		case OPT_CONTENT:	
			cursor=DatabaseHelper.getInstance(getContext()).query(TBL_Content.getName(), projection, selection, selectionArgs, null, null, null);
			break;
		case OPT_APP:
			cursor=DatabaseHelper.getInstance(getContext()).query(TBL_App.getName(), projection, selection, selectionArgs, null, null, null);
			break;

		default:
			break;
		}
		if(cursor!=null)
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int match=sUriMatcher.match(uri);
		int  rowEffect=0;
		switch (match) {
		case OPT_CONTENT:	
			rowEffect=DatabaseHelper.getInstance(getContext()).update(TBL_Content.getName(), values, selection, selectionArgs);
			break;

		case OPT_APP:
			rowEffect=DatabaseHelper.getInstance(getContext()).update(TBL_App.getName(), values, selection, selectionArgs);
			break;

		default:
			break;
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return rowEffect;
	}

}

